package com.dhecp.project.system.controller;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import com.dhecp.core.utils.qywx.QyWxApiUrls;
import com.dhecp.core.utils.qywx.QyWxHttpUtils;
import com.dhecp.core.utils.qywx.WeChatUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.dhecp.common.core.domain.AjaxResult;

@Controller
@RequestMapping("/wechat")
public class WeChatController {
    /**  获取最新企业微信凭证，过期则更新
     * @return systemSetting
     */
    @RequestMapping("/getTicket")
    @ResponseBody
    public AjaxResult getTicket() {
        String ticket =  WeChatUtils.getTicket(WeChatUtils.getToken(WeChatUtils.APPID,WeChatUtils.H5SECERT, WeChatUtils.H5CACHEKEY));
        String corpId = WeChatUtils.getCorpId();
        String agentId= WeChatUtils.getAgentId();
        Map<String, Object> dataMap = new HashMap<>();
        dataMap.put("corpId", corpId);
        dataMap.put("agentId", agentId);
        dataMap.put("ticket", ticket);
        return AjaxResult.success(dataMap);
    }

    /**  手动更新企业微信凭证
     * @return systemSetting
     */
    @RequestMapping("/updateTicket")
    @ResponseBody
    public AjaxResult updateTicket() {
        String ticket =  WeChatUtils.updateTicket(WeChatUtils.getToken(WeChatUtils.APPID,WeChatUtils.H5SECERT, WeChatUtils.H5CACHEKEY));
        return AjaxResult.success(ticket);
    }

    /**  获取访问用户身份
     * @return AjaxResult
     */
    @RequestMapping("/getUserInfo")
    @ResponseBody
    public AjaxResult queryUserInfo(@RequestParam Map<String, Object> params) {
        String token = WeChatUtils.getToken(WeChatUtils.APPID,WeChatUtils.H5SECERT, WeChatUtils.H5CACHEKEY);
        String requestUrl = QyWxApiUrls.USERINFO_GET_URL.replace("ACCESS_TOKEN", token).replace("CODE", params.get("code").toString());//填充凭证
        JSONObject jsonObject = QyWxHttpUtils.sendGet(requestUrl);//发起请求
        return AjaxResult.success(jsonObject);
    }

    /**  发送应用消息
     * @return AjaxResult
     */
    @RequestMapping("/send")
    @ResponseBody
    public AjaxResult send(@RequestParam Map<String, Object> params) {
        String token = WeChatUtils.getToken(WeChatUtils.APPID,WeChatUtils.H5SECERT, WeChatUtils.H5CACHEKEY);
        //发起请求
        String requestUrl = QyWxApiUrls.MESSAGE_SEND_URL.replace("ACCESS_TOKEN", token);
        String[] keyArr = {"touser","toparty","totag","agentid","safe","enable_id_trans","enable_duplicate_check","duplicate_check_interval","msgtype"};
        for(String key : params.keySet()) {
            if(!Arrays.asList(keyArr).contains(key)) {
                params.put(key, JSONArray.parse(params.get(key).toString()));
            }
        }
        String postStr = JSONArray.toJSONString(params);
        JSONObject jsonObject = QyWxHttpUtils.sendPost(requestUrl, postStr);
        return AjaxResult.success(jsonObject);
    }
}